import pygame
from pygame.locals import *
import random

#定义屏幕宽，高
WIDTH = 480
HEIGHT = 600
#创建颜色
COLOR_GREEN = (0,255,0)
COLOR_BLACK = (0,0,0)
COLOR_YELLOW = (255, 255, 0)
COLOR_RED = (255, 0, 0)
COLOR_WHITE = (255, 255, 255)
#刷新频率
FPS = 60
#初始化
pygame.init()

#####定义精灵区域：#################
class Player(pygame.sprite.Sprite):
    def __init__(self):
        #完成精灵初始化
        pygame.sprite.Sprite.__init__(self)
        #初始化精灵的图像
        self.image = pygame.Surface((50,50))
        #初始化精灵的颜色
        self.image.fill(COLOR_GREEN)
        #设置角色的长方形
        self.rect = self.image.get_rect()
        #设置长方形中心的X，Y值
        self.rect.centerx = WIDTH/2
        self.rect.centery = HEIGHT - 10
        
    def update(self):
        self.speedx = 0
        self.speedy = 0
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            self.speedx = -8
        if keys[pygame.K_RIGHT]:
            self.speedx = 8
        if keys[pygame.K_UP]:
            self.speedy = -8
        if keys[pygame.K_DOWN]:
            self.speedy = 8
        self.rect.x += self.speedx
        self.rect.y += self.speedy
        #边缘检测
        if self.rect.right > WIDTH:
            self.rect.right = WIDTH
        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.bottom > HEIGHT:
            self.rect.bottom = HEIGHT
        if self.rect.y < 0:
            self.rect.y = 0

    def shoot(self):
        bullet = Bullet(self.rect.centerx, self.rect.top)
        all_sprites.add(bullet)
        all_bullets.add(bullet)
####################################
class Meteorite(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((30, 40))
        self.image.fill(COLOR_RED)
        self.rect = rect = self.image.get_rect()
        self.rect.x = random.randrange(WIDTH-self.rect.width)
        self.rect.y = random.randrange(-100, 40)

        self.speedy = random.randrange(1, 8)
        self.speedx = random.randrange(-3, 3)
    def update(self):
        self.rect.x += self.speedx
        self.rect.y += self.speedy
        if self.rect.top > HEIGHT + 10 or self.rect.left < -30 or self.rect.right > WIDTH + 30:
            self.rect.x = random.randrange(WIDTH-self.rect.width)
            self.rect.y = random.randrange(-100, 40)

class Bullet(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((10, 20))
        self.image.fill(COLOR_YELLOW)
        self.rect = self.image.get_rect()
        # 定位子弹位置
        self.rect.y = y
        self.rect.centerx = x

        self.speedy = -10
    
    def update(self):
        self.rect.y += self.speedy
        if self.rect.bottom < 0:
            self.kill()
        


#创建屏幕
screen = pygame.display.set_mode((WIDTH,HEIGHT))
def draw_text(surf,text, size, x, y):
    font_name = pygame.font.match_font("黑体")
    font = pygame.font.Font(font_name, size)
    text_surface = font.render(text, True, COLOR_WHITE)
    text_rect = text_surface.get_rect()
    text_rect.midtop = (x, y)
    surf.blit(text_surface, text_rect)


score = 0
life = 3
# 初始化计时器
clock = pygame.time.Clock()
#实例化飞机对象
player = Player()

all_sprites = pygame.sprite.Group()
all_bullets = pygame.sprite.Group()
all_meteorite = pygame.sprite.Group()
all_sprites.add(player)
for i in range(8):
    met = Meteorite()
    all_sprites.add(met)
    all_meteorite.add(met)

running = True
while running:
    clock.tick(FPS)
    screen.fill(COLOR_BLACK)
    all_sprites.update()
    #将飞机对象绘制到屏幕上
    all_sprites.draw(screen)
    # screen.blit(player.image,player.rect)
    hits = pygame.sprite.groupcollide(all_meteorite, all_bullets, True, True)
    for hit in hits:
        score += 5
        meteorite = Meteorite()
        all_sprites.add(meteorite)
        all_meteorite.add(meteorite)
    #重绘界面，相当于刷新
    hits = pygame.sprite.spritecollide(player, all_meteorite, True)
    if hits:
        life -= 1
        if not life:
            running = False

    
    #关闭游戏检测:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False
            elif event.key == pygame.K_SPACE:
                player.shoot()
    draw_text(screen, str(score), 30, WIDTH / 2, 10)
    draw_text(screen, str(life), 30, WIDTH / 3, 10)
    pygame.display.flip()


#彻底退出
pygame.quit()                
